package com.macrolab.util;

import org.apache.velocity.app.Velocity;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

public class MyTools {


    public static boolean isNotEmpty(Object o) {
        return !isEmpty(o);
    }

    /**
     *
     * @param o String,List,Map,Object[],int[],long[]
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static boolean isEmpty(Object o) {
        if (o == null) {
            return true;
        }
        if (o instanceof String) {
            if (o.toString().trim().equals("")) {
                return true;
            }
        } else if (o instanceof List) {
            if (((List) o).size() == 0) {
                return true;
            }
        } else if (o instanceof Map) {
            if (((Map) o).size() == 0) {
                return true;
            }
        } else if (o instanceof Set) {
            if (((Set) o).size() == 0) {
                return true;
            }
        } else if (o instanceof Object[]) {
            if (((Object[]) o).length == 0) {
                return true;
            }
        } else if (o instanceof int[]) {
            if (((int[]) o).length == 0) {
                return true;
            }
        } else if (o instanceof long[]) {
            if (((long[]) o).length == 0) {
                return true;
            }
        }
        return false;
    }


    /**
     *
     * @param srcName
     * @param firstUpper
     * @return
     */
    public static String buildCamelName(String srcName, boolean firstUpper) {
        String[] nameSegs = srcName.toLowerCase().split("_");
        String name = "";
        for (int i = 0; i < nameSegs.length; i++) {
            String nameSeg = nameSegs[i];
            // System.out.println("nameSegs[" + i + "] = " + nameSeg);
            if (firstUpper) {
                firstUpper = false;
                nameSeg = (nameSeg.length() > 1) ? (nameSeg.substring(0, 1).toUpperCase() + nameSeg.substring(1))
                        : nameSeg.toUpperCase();
            } else {
                if (i == 0) {
                    nameSeg = nameSeg.toLowerCase();
                } else {

                    nameSeg = (nameSeg.length() > 1) ? (nameSeg.substring(0, 1).toUpperCase() + nameSeg.substring(1))
                            : nameSeg.toUpperCase();
                }
            }

            name += nameSeg;
        }
        return name;
    }


    public static void createTmpDir(String workDir) {
        File dir = new File(workDir);
        boolean isDirCreated = dir.mkdirs();
    }

    /**
     * Date to String.
     *
     * @param date
     * @return yyyy-MM-dd HH:mm:ss
     */
    static public String date2String(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(date);
    }

    /**
     * Date to string.
     *
     * @param date
     * @param pattern yyyy-MM-dd HH:mm:ss, yyyy-MM-dd ...
     * @return date string
     */
    public static String date2String(Date date, String pattern) {
        return new SimpleDateFormat(pattern, Locale.getDefault()).format(date);
    }

    public static String date2String(long date) {
        return date2String(new Date(date));
    }

    /**
     * init velocity engine
     */
    public static  void initVelocity() {
        Properties p = new Properties();
        try {
            // load *.vm template file in classpath
            p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
            // define charset
            p.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
            p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
            // iniit Velocity engine by Properties
            Velocity.init(p);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}


